<template>
  <div>
    <el-table v-loading="loading" :data="detailsList">
      <el-table-column label="商品名称" align="center" prop="ticketName" />
      <el-table-column label="缩略图" align="center" prop="goodPic">
        <template v-slot="scope">
          <image-preview :src="scope.row.goodPic" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="规格" align="center" prop="ticketTypeName" />
      <el-table-column label="数量" align="center" prop="ticketCount" />
      <el-table-column label="金额" align="center" prop="ticketMoney" >
        <template v-slot="scope">
          <span>{{(scope.row.ticketMoney / 100).toFixed(2)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="物流单号" align="center" prop="trackingCode" />
      <el-table-column label="状态" align="center" prop="ticketStatus" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.order_status" :value="scope.row.ticketStatus"/>
          </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="130">
        <template slot-scope="scope">
        <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleOrderNo(scope.row)"
          >回填物流单号</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 回填物流单号 -->
    <el-dialog title="回填物流单号" :visible.sync="orderNoDialogVisible" width="500px" append-to-body :close-on-click-modal="dialogIsCloseOnModal" :close-on-press-escape="dialogIsCloseOnModal">
      <el-form ref="formOrder" :model="formOrder" :rules="rulesOrder" label-width="80px">
        <el-form-item label="物流单号" prop="orderNo">
          <el-input v-model="formOrder.orderNo" placeholder="请输入物流单号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitOrderNo">确 定</el-button>
        <el-button @click="cancelOrder">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDetails, backfill } from "@/api/order/details";

export default {
  name: "ShowOrderDetails",
  dicts:['order_status'],
  props:{
    pOrderId: {
      type: [Number, String],
      default: ''
    }
  },
  data() {
    return {
      // 详情弹框
      detailDialogShow:false,
      orderNoDialogVisible:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单详情表格数据
      detailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ticketId: null,
        ticketName: null,
        orderTicketType: null,
        orderDetailsNum: null,
        orderId: null,
        orderNum: null,
        ticketTypeId: null,
        ticketMoney: null,
        ticketCount: null,
        ticConfigId: null,
        ticketUsetimeStart: null,
        ticketUsetimeEnd: null,
        ticketStatus: null,
        ticketTypeName: null,
        shopDetails: null
      },
      // 表单参数
      form: {},
      formOrder:{
        orderNo:'',
        orderDetailId:''
      },
      // 表单校验
      rules: {
      },
      rulesOrder:{
        orderNo:[
           { required: true, trigger: "blur", message: "请输入物流单号" },
        ]
      },
      orderId: this.pOrderId
    };
  },
  watch: {
    pOrderId(val) {
      console.log("porderId:", val)
      if (val) {
        this.orderId = val;
        this.getList(this.orderId)
      }
    }
  },
  created() {
    this.getList(this.orderId)
  },
  methods: {
    isOpen(orderId){
      // 弹框形式
      this.getList(orderId);
      this.detailDialogShow=true

    },
    handleOrderNo(row){
      this.orderNoDialogVisible=true
      this.formOrder.orderDetailId=row.orderDetailsId
      this.formOrder.orderNo=row.trackingCode || ''
    },
    submitOrderNo(){
      this.$refs["formOrder"].validate(valid => {
        if (valid) {
          let params={
            orderDetailId:this.formOrder.orderDetailId,
            trackingCode:this.formOrder.orderNo
          }
          console.log(params)
          backfill(params).then(res=>{
            this.getList()
            this.cancelOrder()
          })
        }
      })
    },
    cancelOrder(){
      this.orderNoDialogVisible=false
      this.formOrder.orderNo=''
      this.resetForm("formOrder");
    },
    /** 查询订单详情列表 */
    getList(orderId) {
      this.loading = true;
      let temp=orderId || this.orderId
      listDetails(temp).then(response => {
        this.detailsList = response.data;
        // this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderDetailsId: null,
        ticketId: null,
        ticketName: null,
        orderTicketType: null,
        orderDetailsNum: null,
        orderId: null,
        orderNum: null,
        ticketTypeId: null,
        ticketMoney: null,
        ticketCount: null,
        ticConfigId: null,
        ticketUsetimeStart: null,
        ticketUsetimeEnd: null,
        ticketStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        ticketTypeName: null,
        shopDetails: null
      };
      this.resetForm("form");
    },
  }
};
</script>
